//
//  MJTUIView.swift
//  swiftWeiBo
//
//  Created by MJT on 16/7/9.
//  Copyright © 2016年 MJT. All rights reserved.
//

import UIKit
import SnapKit

class MJTUIView: UIView {
    //设置闭包
    var loginClosure:(() -> ())?
    //懒加载控件
    //懒加载旋转图片
    private lazy var cycleImageView: UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_smallicon"))
    //设置遮罩
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //设置主页图片
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //消息
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(14)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.grayColor()
        label.numberOfLines = 0
        //居中显示
        label.textAlignment = .Center

        return label
    }()
    //懒加载设置注册
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        //设置文字
        button.setTitle("注册", forState: .Normal)
        button.addTarget(self, action: "registerButtonAction", forControlEvents: .TouchUpInside)
        //设置颜色
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        //设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)

        return button
    }()
    //懒加载设置登录
    private lazy var logInButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "loginButtonAction", forControlEvents: .TouchUpInside)
        //设置文字
        button.setTitle("登录", forState: .Normal)
        //设置颜色
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        //设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        return button
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置UI
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI() {
        //设置访客视图颜色
        backgroundColor = UIColor(white: 237 / 255,alpha: 1)
        //添加子视图
        //动画视图
        addSubview(cycleImageView)
        //遮罩视图
        addSubview(maskImageView)
        //房子视图
        addSubview(iconImageView)
        //信息label
        addSubview(messageLabel)
        //注册按钮
        addSubview(registerButton)
        //登录按钮
        addSubview(logInButton)
        //添加约束
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.width.equalTo(224)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        logInButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
//        startAnimation()

    }
    //注册按钮的点击事件
    @objc private func registerButtonAction() {
        print("注册按钮")
        loginClosure!()
    }
    //访客登录按钮点击事件
    @objc private func loginButtonAction() {
        print("登录按钮")
        loginClosure?()
    }

    //旋转动画
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //目的地
        animation.toValue = 2 * M_PI
        //动画时长
        animation.duration = 20
        //重复次数
        animation.repeatCount = MAXFLOAT
        //动画执行完毕后不让其释放
        animation.removedOnCompletion = false
        //执行动画
        cycleImageView.layer.addAnimation(animation, forKey: nil)
    }

    //修改访客视图中心图片&文字信息
    func updateVisitorViewInfo(title: String? ,imageName: String?){
        if let t = title, imaName = imageName {
            messageLabel.text = t
            iconImageView.image = UIImage(named: imaName)
            cycleImageView.hidden = true
        } else {
            //开启动画
            startAnimation()
            cycleImageView.hidden = false
        }

    }


    private func text() {
        //手动布局
        //        registerButton.layouts
        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
    }
}